
"""
Created on Tue Feb 19 14:55:43 2013

@author: aponteeduarddo@gmail.com
Copyright Eduardo Aponte 2013.
"""

from mex_binder import binder_py

eyelink_newest_float_sample = binder_py('mx_eyelink_newest_float_sample')
eyelink_newest_float_sample.mx_c.headers = ['<eyelink.h>','"mex.h"','"utils.h"']
eyelink_newest_float_sample.bindo += ['utils.o']
eyelink_newest_float_sample.mx_c.code = \
"""
INT16 _main( mxArray **ia )
{
    void FARTYPE *buf;
    INT16 code;
    code = eyelink_get_newest_float_sample( buf );
    switch ( code ) {
        mwSize t[1] = {0};
        case 0:
            *ia = mxCreateCellArray(0,t); /* Return empty array */
            break;
        case -1:
            *ia = mxCreateCellArray(0,t); /* Return empty array */
            break;
        default:
            fsample2struct( *((FSAMPLE *) buf), ia );
            break;
    }
    return code;
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;

    /* Verify input arguments. */
    if(nrhs != 0 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 2 ) {
        mexErrMsgTxt("Too many output arguments.");
    }

    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status = mxGetPr(plhs[0]);

    if ( nlhs == 2 ){
        *status = (double ) _main( plhs+1 );
    } else if ( nlhs < 2 ){
        *status = (double ) eyelink_get_newest_float_sample( NULL );
    }
    return;
}
"""

eyelink_newest_float_sample.mx_c.mdoc = \
"""function [ ] = mx_eyelink_newest_float_sample( )
%MX_EYELINK_NEWEST_FLOAT_SAMPLE Check if a new sample has arrived from the link. 
% This is the latest sample, not the oldest sample that is read by 
% eyelink_get_next_data(), and is intended to drive gaze cursors and 
% gaze-contingent displays. Typically the function is called with a NULL buffer
% pointer, to test if new data has arrived. If a value of 1 is returned, the 
% function is called with a FSAMPLE buffer to get the new sample.
%
% Parameters:
% buf 	If NULL, just checks new-sample.
% status.
% Returns:
% -1 if no samples, 0 if no new data, 1 if new sample.
%
"""
eyelink_newest_float_sample.write_c()
eyelink_newest_float_sample.write_m()
eyelink_newest_float_sample.compile()
eyelink_newest_float_sample.write_sh()

